package com.yvon.maple.cloud.web.handler;

import cn.hutool.core.util.CharsetUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yvon.boot.common.result.GlobalException;
import com.yvon.boot.common.result.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * http处理程序
 *
 * @author Yvon
 * @since 2023-01-05
 */
@Slf4j
@Component
public class HttpHandler {


	@Autowired
	private ObjectMapper objectMapper;

	public <T> void printServerResponseToWeb(ResponseData<T> serverResponseEntity) {
		if (serverResponseEntity == null) {
			log.info("print obj is null");
			return;
		}

		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		if (requestAttributes == null) {
			log.error("requestAttributes is null, can not print to web");
			return;
		}
		HttpServletResponse response = requestAttributes.getResponse();
		if (response == null) {
			log.error("httpServletResponse is null, can not print to web");
			return;
		}
		log.error("response error: " + serverResponseEntity.getMessage());
		response.setCharacterEncoding(CharsetUtil.UTF_8);
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.write(objectMapper.writeValueAsString(serverResponseEntity));
		}
		catch (IOException e) {
			throw new GlobalException(e, "io 异常");
		}
	}

}
